package backtrack;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description: 17. 电话号码的字母组合
 * User: Li_yizYa
 * Date: 2025/6/12
 * Time: 22:45
 */
public class Solution3 {
    public List<String> letterCombinations(String digits) {
        List<String> ret = new ArrayList<>();
        if (digits == null || digits.length() == 0) {
            return ret;
        }
        String[] table = {"", "", "abc", "def", "ghi", "jkl", "mno", "pqrs", "tuv", "wxyz"};
        StringBuilder tmp = new StringBuilder();
        backTracking(ret, table, digits, tmp, 0);
        return ret;
    }

    private void backTracking(List<String> ret, String[] table, String digits, StringBuilder tmp, int index) {
        if (tmp.length() == digits.length()) {
            ret.add(tmp.toString());
            return;
        }
        String str = table[digits.charAt(index) - '0'];
        for (int i = 0; i < str.length(); i++) {
            tmp.append(str.charAt(i));
            backTracking(ret, table, digits, tmp, index + 1);
            tmp.deleteCharAt(tmp.length() - 1);
        }
    }
}
